
package simuri.uridb;

import com.hp.hpl.jena.query.*;
import org.apache.log4j.Logger;

/**
 *
 * @author ngonga
 */
public class EdgeCountCoherence {
    
    static Logger logger = Logger.getLogger("AEON");
    public int DIRECT_EDGE_FACTOR = 10;

    /** Computes the log of the number of edges between uri1 and uri2
     *
     * @param uri1 First URI
     * @param uri2 Second URI
     * @param endpoint SPARQL Endpoint
     * @param graph Graph (null if not needed)
     * @return Score
     */

    public double getCoherence(String uri1, String uri2, String endpoint, String graph) {
        if (uri1.equals(uri2)) {
            return -1;
        }
        //first get direct edges
        
        String query = "SELECT ?p WHERE {{<" + uri1 + "> ?p <" + uri2 + ">} UNION {<" + uri2 + "> ?p <" + uri1 + ">}}";
        Query sparqlQuery = QueryFactory.create(query);
        QueryExecution qexec;
        if (graph != null) {
            qexec = QueryExecutionFactory.sparqlService(endpoint, sparqlQuery, graph);
        } else {
            qexec = QueryExecutionFactory.sparqlService(endpoint, sparqlQuery);
        }
        ResultSet results;
        int count = 0;
        boolean done = false;
        while (!done) {
            try {
                count = 0;
                results = qexec.execSelect();
                while (results.hasNext()) {
                    QuerySolution soln = results.nextSolution();
                    count++;
                }
                done = true;
            } catch (Exception ex) {
                ex.printStackTrace();
                logger.warn(ex.getMessage());
            }

        }
        count = count * DIRECT_EDGE_FACTOR;

        return Math.log(count + 1);

  }
    
}
